/**************************************************************
 *@ProjectName: blweibo
 *@FileName: DetailWeiboActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.text.Html;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiweibo.models.Count;
import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.models.Topic;
import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.TopicUtil;

/**
 * The Class DetailWeiboActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class DetailWeiboActivity extends BaseActivity implements OnClickListener,
        android.content.DialogInterface.OnClickListener, OnLongClickListener {

    /** The str topic id. */
    private String strTopicId;

    /** The m topic. */
    private Topic mTopic;

    /** The tv via. */
    private TextView tvVia;

    /** The tv message. */
    private TextView tvMessage;

    /** The tv profile name. */
    private TextView tvProfileName;

    /** The tv updated. */
    private TextView tvUpdated;

    /** The tv reload. */
    private TextView tvReload;

    /** The tv fav. */
    private TextView tvFav;

    /** The tv more. */
    private TextView tvMore;

    /** The m task. */
    private DetailTask mTask;

    /** The tv comment. */
    private TextView tvComment;

    /** The tv forward. */
    private TextView tvForward;

    /** The tweet_ori txt. */
    private TextView tweet_oriTxt;

    /** The src_text_block. */
    private LinearLayout src_text_block;

    /** The tweet_upload_pic. */
    private ImageView tweet_upload_pic;

    /** The tweet_upload_pic2. */
    private ImageView tweet_upload_pic2;

    /** The relative layout01. */
    private RelativeLayout relativeLayout01;

    /** The tweet_profile_preview. */
    private ImageView tweet_profile_preview;

    /** The share items. */
    private String[] shareItems;

    /** The share dialog. */
    private Dialog shareDialog;

    /** The src choice dialog. */
    private AlertDialog srcChoiceDialog;

    /** The reason choice dialog. */
    private AlertDialog reasonChoiceDialog;

    /** The tweet_redirect. */
    private Button tweet_redirect;

    /** The tweet_comment. */
    private Button tweet_comment;

    /** The tweet_redirect_rt. */
    private TextView tweet_redirect_rt;

    /** The tweet_comment_rt. */
    private TextView tweet_comment_rt;

    /** The tweet_redirect_rt_ly. */
    private LinearLayout tweet_redirect_rt_ly;

    /** The tweet_comment_rt_ly. */
    private LinearLayout tweet_comment_rt_ly;

    /** The iv v. */
    private ImageView ivV;

    /** The iv hr. */
    private ImageView ivHR;

    /** The iv li. */
    private ImageView ivLi;

    /** The iv qi. */
    private ImageView ivQi;

    /** The tweet_profile. */
    private RelativeLayout tweet_profile;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 0:
            startActivity(new Intent(this, MainTabActivity.class));
            finish();
            break;
        case 1:
            finish();
            break;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onClick(android.view.View)
     */
    @Override
    public void onClick(View paramView) {
        if (paramView == tvFav) {
            new FavTask().execute(0);
        } else if (paramView == tvReload) {
            mTask = new DetailTask();
            mTask.execute(new Object[0]);
        } else if (paramView == tvComment) {
            Intent intent = new Intent(DetailWeiboActivity.this, EditActivity.class);
            intent.putExtra("mode", 1);
            intent.putExtra("topicId", strTopicId);
            startActivity(intent);
        } else if (paramView == tvForward) {
            Intent intent = new Intent(DetailWeiboActivity.this, EditActivity.class);
            intent.putExtra("mode", 2);
            intent.putExtra("topicId", strTopicId);
            if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
                intent.putExtra("content", "//@" + mTopic.getUser().getUsername() + ":" + mTopic.getHText());
            }
            startActivity(intent);
        } else if (paramView == tvMore) {
            Intent it = new Intent();
            Context localContext = getApplicationContext();
            Intent localIntent2 = it.setClass(localContext, ChoiceActivity.class).putExtra(
                    "com.hbr.ChoiceActivity.EXTRA_LAUCH_MODE", 1);
            if (mTopic.getUser().getUid().equals(StaticInfo.mUid)) {
                localIntent2.putExtra("EXTRA_IS_SELF", true);
            }
            startActivityForResult(it, 1000);
        } else if (paramView == tweet_upload_pic) {
            startActivity(new Intent(DetailWeiboActivity.this, ImageViewer.class).putExtra("picUrl", mTopic.getPic_a()));
        } else if (paramView == tweet_upload_pic2) {
            startActivity(new Intent(DetailWeiboActivity.this, ImageViewer.class).putExtra("picUrl", mTopic
                    .getRetopic().getPic_a()));
        } else if (paramView == tweet_profile) {
            startActivity(new Intent(DetailWeiboActivity.this, UserInfoActivity2.class).putExtra("user",
                    mTopic.getUser()));
        } else if (paramView == tweet_redirect) {
            Intent intent = new Intent(DetailWeiboActivity.this, EditActivity.class);
            intent.putExtra("mode", 2);
            intent.putExtra("topicId", strTopicId);
            if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
                intent.putExtra("content", "//@" + mTopic.getUser().getUsername() + ":" + mTopic.getHText());
            }
            startActivity(intent);
        } else if (paramView == tweet_comment) {
            Intent intent = new Intent(DetailWeiboActivity.this, CommentBlog.class);
            intent.putExtra("topicId", strTopicId);
            startActivity(intent);
        } else if (paramView == tweet_redirect_rt_ly) {
            Intent intent = new Intent(DetailWeiboActivity.this, EditActivity.class);
            intent.putExtra("mode", 2);
            intent.putExtra("topicId", strTopicId);
            startActivity(intent);
        } else if (paramView == tweet_comment_rt_ly) {
            Intent intent = new Intent(DetailWeiboActivity.this, CommentBlog.class);
            intent.putExtra("topicId", mTopic.getRetopic().getTid());
            startActivity(intent);
        } else {
            super.onClick(paramView);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnLongClickListener#onLongClick(android.view.View)
     */
    @Override
    public boolean onLongClick(View v) {
        if (v == tvMessage) {
            showDialog(1002);
        } else if (v == tweet_oriTxt) {
            showDialog(1003);
        }
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onActivityResult(int, int,
     * android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1 && requestCode == 1000) {
            processChoiceResult(data);
        }
    }

    /**
     * Process choice result.
     * 
     * @param paramIntent
     *            the param intent
     */
    private void processChoiceResult(Intent paramIntent) {
        if (paramIntent != null) {
            switch (paramIntent.getIntExtra("EXTRA_RESULT_SELECTED", -1)) {
            case 1:
                showDialog(1);
                break;
            case 2:
                showDialog(1001);
                break;
            case 3:
                break;
            case 4:
                break;
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int paramInt) {
        switch (paramInt) {
        case 1:
            shareItems = getResources().getStringArray(R.array.share_dialog_list);
            shareDialog = new AlertDialog.Builder(this).setItems(shareItems, this).setTitle(R.string.share).create();
            return shareDialog;
        case 1001:
            return new AlertDialog.Builder(this).setTitle(getString(R.string.attention_all))
                    .setMessage(getString(R.string.delete_weibo_or_not))
                    .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            new DeleteWeiboTask().execute(0);
                        }
                    }).setNegativeButton(getString(R.string.cancel), null).create();
        case 1002:
            srcChoiceDialog = new AlertDialog.Builder(DetailWeiboActivity.this)
                    .setTitle(getString(R.string.attention_all))
                    .setItems(getResources().getStringArray(R.array.reason_dialog_list), this).create();
            return srcChoiceDialog;
        case 1003:
            reasonChoiceDialog = new AlertDialog.Builder(DetailWeiboActivity.this)
                    .setTitle(getString(R.string.attention_all))
                    .setItems(getResources().getStringArray(R.array.src_dialog_list), this).create();
            return reasonChoiceDialog;
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.detailweibo);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.title_mblog_content),
                getString(R.string.main_home));
        mTopic = (Topic) getIntent().getSerializableExtra("topic");
        strTopicId = mTopic.getTid();
        tvVia = (TextView) findViewById(R.id.tweet_via);
        tvMessage = (TextView) findViewById(R.id.tweet_message);
        tvProfileName = (TextView) findViewById(R.id.tweet_profile_name);
        tvUpdated = (TextView) findViewById(R.id.tweet_updated);
        tvReload = (TextView) findViewById(R.id.tvReload);
        tvComment = (TextView) findViewById(R.id.tvComment);
        tvForward = (TextView) findViewById(R.id.tvForward);
        tweet_oriTxt = (TextView) findViewById(R.id.tweet_oriTxt);
        src_text_block = (LinearLayout) findViewById(R.id.src_text_block);
        tweet_upload_pic = (ImageView) findViewById(R.id.tweet_upload_pic);
        tweet_upload_pic2 = (ImageView) findViewById(R.id.tweet_upload_pic2);
        relativeLayout01 = (RelativeLayout) findViewById(R.id.RelativeLayout01);
        tweet_profile_preview = (ImageView) findViewById(R.id.tweet_profile_preview);
        tvFav = (TextView) findViewById(R.id.tvFav);
        tvMore = (TextView) findViewById(R.id.tvMore);
        tweet_redirect = (Button) findViewById(R.id.tweet_redirect);
        tweet_comment = (Button) findViewById(R.id.tweet_comment);
        tweet_redirect_rt = (TextView) findViewById(R.id.tweet_redirect_rt);
        tweet_comment_rt = (TextView) findViewById(R.id.tweet_comment_rt);
        tweet_redirect_rt_ly = (LinearLayout) findViewById(R.id.tweet_redirect_rt_ly);
        tweet_comment_rt_ly = (LinearLayout) findViewById(R.id.tweet_comment_rt_ly);
        ivV = (ImageView) findViewById(R.id.tweet_profile_vip);
        ivHR = (ImageView) findViewById(R.id.tweet_profile_hr);
        ivLi = (ImageView) findViewById(R.id.tweet_profile_li);
        ivQi = (ImageView) findViewById(R.id.tweet_profile_qi);
        tweet_profile = (RelativeLayout) findViewById(R.id.tweet_profile);
        tvFav.setOnClickListener(this);
        tvMore.setOnClickListener(this);
        tvReload.setOnClickListener(this);
        tvForward.setOnClickListener(this);
        tvComment.setOnClickListener(this);
        tvMessage.setOnLongClickListener(this);
        tweet_profile.setOnClickListener(this);
        tweet_comment.setOnClickListener(this);
        tweet_redirect.setOnClickListener(this);
        tweet_oriTxt.setOnLongClickListener(this);
        tweet_upload_pic.setOnClickListener(this);
        tweet_upload_pic2.setOnClickListener(this);
        tweet_comment_rt_ly.setOnClickListener(this);
        tweet_redirect_rt_ly.setOnClickListener(this);
        tweet_profile_preview.setOnClickListener(this);
        setUI();
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        setFav();
        tvVia.setText(TopicUtil.formatFrom(mTopic.getFrom()));
        tvMessage.setText(Html.fromHtml(mTopic.getText()), TextView.BufferType.SPANNABLE);
        TopicUtil.highlightContent(DetailWeiboActivity.this, (Spannable) tvMessage.getText(), true);
        tvMessage.setMovementMethod(LinkMovementMethod.getInstance());
        tvProfileName.setText(mTopic.getUser().getUsername());
        tvUpdated.setText(mTopic.getDateline());
        User user = mTopic.getUser();
        if (user.getV()) {
            ivV.setVisibility(View.VISIBLE);
        }
        if (user.getHR()) {
            ivHR.setVisibility(View.VISIBLE);
        }
        if (user.getL()) {
            ivLi.setVisibility(View.VISIBLE);
        }
        if (user.getQ()) {
            ivQi.setVisibility(View.VISIBLE);
        }
        new FetchImage(tweet_profile_preview, mTopic.getUser().getFace()).execute(0);
        String strPic = mTopic.getPic_o();
        if (!TextUtils.isEmpty(strPic)) {
            relativeLayout01.setVisibility(View.VISIBLE);
            tweet_upload_pic.setVisibility(View.VISIBLE);
            new FetchImage(tweet_upload_pic, strPic).execute(new Object());

        }
        new CountTask(mTopic.getTid(), tweet_comment, tweet_redirect).execute(0);
        if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
            src_text_block.setVisibility(View.VISIBLE);
            tweet_oriTxt.setText(
                    Html.fromHtml("@" + mTopic.getRetopic().getUser().getUsername() + ":"
                            + mTopic.getRetopic().getHText()), TextView.BufferType.SPANNABLE);
            TopicUtil.highlightContent(DetailWeiboActivity.this, (Spannable) tweet_oriTxt.getText(), true);
            tweet_oriTxt.setMovementMethod(LinkMovementMethod.getInstance());
            String strPic2 = mTopic.getRetopic().getPic_o();
            if (!TextUtils.isEmpty(strPic2)) {
                tweet_upload_pic2.setVisibility(View.VISIBLE);
                new FetchImage(tweet_upload_pic2, strPic2).execute(new Object());

            }
            new CountTask(mTopic.getRetopic().getTid(), tweet_comment_rt, tweet_redirect_rt).execute(0);
        }

    }

    /**
     * Sets the fav.
     */
    private void setFav() {
        if (mTopic.getFavorited()) {
            tvFav.setText(R.string.itemmenu_bookmark_del);
        } else {
            tvFav.setText(R.string.itemmenu_bookmark);
        }
    }

    /**
     * The Class DetailTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    public class DetailTask extends AsyncTask<Object, Integer, Object> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            String strUrl = String.format(Constant.SHOW, strTopicId, StaticInfo.mUid);
            mTopic = HttpUtil.getTopic(DetailWeiboActivity.this, strUrl);
            return mTopic;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(DetailWeiboActivity.this);
            pd.setMessage("取数据中…");
            pd.show();
        }
    }

    /**
     * The Class FetchImage.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchImage extends AsyncTask {

        /** The piv. */
        private ImageView piv;

        /** The str url. */
        private String strUrl;

        /** The bm. */
        private Bitmap bm;

        /**
         * Instantiates a new fetch image.
         * 
         * @param iv
         *            the iv
         * @param url
         *            the url
         */
        public FetchImage(ImageView iv, String url) {
            piv = iv;
            strUrl = url;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            bm = HttpUtil.getImage(DetailWeiboActivity.this, strUrl);
            if (bm != null) {
                int width = bm.getWidth();
                int height = bm.getHeight();
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int newWidth = metrics.widthPixels;
                int newHeight = metrics.heightPixels;
                if (width > newWidth || height > newHeight) {
                    float scaleWidth = ((float) newWidth) / width;
                    Matrix matrix = new Matrix();
                    matrix.postScale(scaleWidth, scaleWidth);
                    bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
                }
            }
            return bm;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            piv.setImageBitmap(bm);
        }

    }

    /**
     * The Class FavTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FavTask extends AsyncTask<Object, Integer, Object> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            if (mTopic.getFavorited()) {
                HttpUtil.exeUrl(DetailWeiboActivity.this, Constant.FAVORITES_DESTROY + mTopic.getTid() + ".json");
                mTopic.setFavorited(false);
            } else {
                HttpUtil.exeUrl(DetailWeiboActivity.this, Constant.FAVORITES_CREATE + "?tid=" + mTopic.getTid());
                mTopic.setFavorited(true);
            }
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            setFav();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(DetailWeiboActivity.this);
            pd.setMessage("请稍候……");
            pd.show();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.content.DialogInterface.OnClickListener#onClick(android.content
     * .DialogInterface, int)
     */
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (shareDialog == dialog) {
            String strA = "@%s:%s\n";
            String strB = "图片地址:";
            String strC = getString(R.string.weibo_host);
            String str2 = shareItems[which];
            if (str2.equals(getString(R.string.short_message))) {
                Uri.Builder localBuilder = new Uri.Builder();
                localBuilder.scheme("sms");
                Uri localUri = localBuilder.build();
                Intent localIntent = new Intent("android.intent.action.SENDTO", localUri);
                StringBuilder sb = new StringBuilder();
                sb.append(String.format(strA, mTopic.getUser().getUsername(), mTopic.getHText()));
                if (!TextUtils.isEmpty(mTopic.getPic_a())) {
                    sb.append(strB).append(mTopic.getPic_a()).append("\n");
                }
                if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
                    sb.append(String.format("转自" + strA, mTopic.getRetopic().getUser().getUsername(), mTopic
                            .getRetopic().getHText()));
                    if (!TextUtils.isEmpty(mTopic.getRetopic().getPic_a())) {
                        sb.append(strB).append(mTopic.getRetopic().getPic_a()).append("\n");
                    }
                }
                sb.append(strC);
                localIntent.putExtra("sms_body", sb.toString());
                startActivity(localIntent);
            } else if (str2.equals(getString(R.string.email))) {
                Intent localIntent = new Intent("android.intent.action.SEND");
                StringBuilder sb = new StringBuilder();
                if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
                    sb.append(String.format(strA, mTopic.getRetopic().getUser().getUsername(), mTopic.getRetopic()
                            .getHText()));
                    if (!TextUtils.isEmpty(mTopic.getRetopic().getPic_a())) {
                        sb.append(strB).append(mTopic.getRetopic().getPic_a()).append("\n");
                    }
                } else {
                    sb.append(String.format(strA, mTopic.getUser().getUsername(), mTopic.getHText()));
                    if (!TextUtils.isEmpty(mTopic.getPic_a())) {
                        sb.append(strB).append(mTopic.getPic_a()).append("\n");
                    }
                }
                sb.append(strC);
                localIntent.putExtra("android.intent.extra.TEXT", getString(R.string.mail_body_text, sb.toString()));
                localIntent.putExtra("android.intent.extra.SUBJECT", getString(R.string.mail_subject));
                localIntent.setType("application/octet-stream");
                startActivity(Intent.createChooser(localIntent, getString(R.string.choose_email_client)));
            }
        } else if (dialog == srcChoiceDialog) {
            ClipboardManager localClipboardManager1 = (ClipboardManager) getSystemService("clipboard");
            localClipboardManager1.setText(tvMessage.getText());
            Toast.makeText(this, R.string.copy_to_clipboard, Toast.LENGTH_SHORT).show();
        } else if (dialog == reasonChoiceDialog) {
            ClipboardManager localClipboardManager1 = (ClipboardManager) getSystemService("clipboard");
            localClipboardManager1.setText(tweet_oriTxt.getText());
            Toast.makeText(this, R.string.copy_to_clipboard, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * The Class DeleteWeiboTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class DeleteWeiboTask extends AsyncTask<Object, Integer, Boolean> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Boolean doInBackground(Object... paramArrayOfVoid) {
            String str = HttpUtil.exeUrl(DetailWeiboActivity.this, Constant.DESTROY + mTopic.getTid() + ".json");
            return str.contains("1");
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Boolean paramBoolean) {
            if (paramBoolean) {
                Toast.makeText(DetailWeiboActivity.this, R.string.succeed_to_delete_weibo, Toast.LENGTH_SHORT).show();
                WeiboDB wd = WeiboDB.getInstance(DetailWeiboActivity.this);
                wd.deleteTopic(mTopic.getTid());
                wd.deleteAt(mTopic.getTid());
                setResult(-1);
                finish();
            } else {
                Toast.makeText(DetailWeiboActivity.this, R.string.fail_to_delete_weibo, Toast.LENGTH_SHORT).show();
            }
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(DetailWeiboActivity.this);
            pd.setMessage("请稍候……");
            pd.show();
        }
    }

    /**
     * The Class CountTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class CountTask extends AsyncTask<Object, Integer, Count> {

        /** The strtid. */
        private String strtid;

        /** The vc. */
        private View vc;

        /** The vd. */
        private View vd;

        /**
         * Instantiates a new count task.
         * 
         * @param tid
         *            the tid
         * @param v
         *            the v
         * @param v2
         *            the v2
         */
        public CountTask(String tid, View v, View v2) {
            strtid = tid;
            vc = v;
            vd = v2;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Count doInBackground(Object... params) {
            return HttpUtil.getcount(strtid);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Count result) {
            if (vc instanceof Button) {
                ((Button) vc).setText("评论" + result.getComments() + "");
            } else if (vc instanceof TextView) {
                ((TextView) vc).setText(result.getComments() + "");
            }
            if (vd instanceof Button) {
                ((Button) vd).setText("转发" + result.getReplys() + "");
            } else if (vd instanceof TextView) {
                ((TextView) vd).setText(result.getReplys() + "");
            }
        }

    }
}
